package com.evil.kingdom.handler.timer;

import com.evil.kingdom.handler.MyWebSocketHandler;
import com.evil.kingdom.handler.SseHandler;
import com.evil.kingdom.pojo.entity.action.Battle;
import com.evil.kingdom.task.timer.BaseTimerTask;
import com.evil.kingdom.task.timer.BattleTimerTask;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.function.Consumer;

/**
 * 战斗 处理器
 *
 * @author liyang
 * @date 2024-07-04 13:43
 */
@Component
@Slf4j
public class BattleTimerTaskHandler extends BaseTimerTaskHandler<BattleTimerTask> {
    public static final Logger battleLogger = LoggerFactory.getLogger("KINGDOM_BATTLE_LOGGER");

    @Resource
    private SseHandler sseHandler;
    @Resource
    private MyWebSocketHandler myWebSocketHandler;

    public List<BattleTimerTask> buildBattleTimerTask(List<Battle> battles, Consumer<String> deadResult) {
        List<BattleTimerTask> tasks = BattleTimerTask.build(battles, c -> {
        }, (t, c, r) -> {
            // r：死亡人物
            if (null != r) {
                // 移除战斗人物
                t.getLayout().remove(r);

                this.getList().stream().filter(e -> e.getT().getPerson().equals(r)).findFirst().ifPresent(e -> {
                    e.stop();
                    // 回传死亡人物名称
                    deadResult.accept(r.getBase().getName());
                });
            }
        });
        battleLogger.info("buildBattleTask|size:{}", tasks.size());
        this.addTasks(tasks);
        return tasks;
    }
}
